<?php

include_once($_SERVER["DOCUMENT_ROOT"]. "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"]. "/include/user/inc.php");

$g_cgival = array();
$g_show = array();
$g_pro = array();
if(! security_check::check_refer())
{
	throw new Exception("来路不明");
}


function check_cgi_pro()
{
	global $g_cgival, $g_show, $g_pro;
	cgi::both($g_cgival['t'],"t","json");
	$g_cgival['t'] = string::un_html($g_cgival['t']);

	cgi::both($g_cgival['callback'], "callback", "");
	$g_cgival['callback'] = string::un_html(string::un_script_code($g_cgival['callback']));
	
	
	cgi::both($g_cgival['uid'],"uid","");
	$g_cgival['uid'] = intval($g_cgival['uid']);
	
	
	cgi::both($g_cgival['type'],"type","add");
}


function get_data()
{
	global $g_cgival, $g_show, $g_pro;
	
	$type = $g_cgival['type'];
	$add_uid = $g_cgival['uid'];
	$db_user_info = new db_user_info();
	
	$user_info = new user_info();
	$is_login = $user_info->is_logined();
	if($is_login)
	{
		$user_detail_info = $user_info->get_cur_user_info();
		$uid = $user_detail_info["uid"];
		
	}
	else
	{
		
		throw new Exception("未登录");
	}	
	
	if($uid == $add_uid )
	{
		throw new Exception("自己都不认识了？！");
	}
	
	if(!$add_uid)
	{
		throw new Exception("没有选择人啊？！");
	}
	$db_user_fans = new db_user_fans();
	$db_user_follow = new db_user_follow();
	
	if($type == "add")
	{
		$check_follow = $db_user_fans->get_item_by_uid_and_fans_uid($uid,$add_uid); //先判断这个人是否关注我
	
		if($check_follow)
		{
			$uid_is_be_followed = 1;
		}
		else
		{
			$uid_is_be_followed =0;
		}
		
		$db_user_follow->insert_user_follow($uid,$add_uid,$uid_is_be_followed);//更新这个我的的关注表
		$db_user_fans->update_user_fans($uid,$add_uid,"",1);//更新这个我的的粉丝表,如果这个用户已经是我粉丝，更新is_followed_by_uid
		
		
		$db_user_fans->insert_user_fans($add_uid,$uid,$uid_is_be_followed);//更新这个用户的粉丝表
		$db_user_follow->update_user_follow($add_uid,$uid,"",$uid_is_be_followed); //更新这个用户的关注表,更新uid_is_be_followed为1，标记这个用户已经被我关注
		
		
		if($check_follow)
		{
			$res = 2;
		}
		else
		{
			$res =1;
		}
	
	}
	else
	{
		
		$db_user_fans->update_user_fans($add_uid,$uid,0); //取消关注，对方粉丝表减少一条数据
		$db_user_fans->update_user_fans($uid,$add_uid, "",0); //更新我的粉丝表里的 is_followed_by_uid,因为我不再关注他
		$db_user_follow->update_user_follow($uid,$add_uid,0); //我关注的列表里减少一条数据
		$db_user_follow->update_user_follow($add_uid,$uid,"",0); //更新被取消关注用户的 关注 uid_is_be_followed
		$res =0;
		
		
	}
	
	//update count_follow count_fans
	$db_user_info = new db_user_info();
	//当前用户的关注数
	$count_follow = $db_user_follow->get_count_by_uid($uid);
	$db_user_info->update_user_count_by_uid($uid,$count_love="",$count_album="",$count_mblog="",$count_video="",$count_text_mblog="",$count_follow);
	
	//被关注用户分数
	$count_fans = $db_user_fans->get_count_by_uid($add_uid);
	$db_user_info->update_user_count_by_uid($uid,$count_love="",$count_album="",$count_mblog="",$count_video="",$count_text_mblog="",$count_follow="",$count_fans);
	
	$g_show["res"] = $res;
}

 
try
{
	check_cgi_pro();
	get_data();

	$g_show["error"] = "0";
	$g_show["errmsg"] = "操作成功";
}
catch (Exception $e)
{
	$g_show["error"] = "1";
	$g_show["errmsg"] = $e->getMessage();
}
show_pro();
exit();


function show_pro()
{
	global $g_cgival, $g_pro, $g_show, $config;
	
	if ($g_cgival['t'] == "serialize")
	{
		echo serialize($g_show);
	}
	elseif ($g_cgival['t'] == "json")
	{
		echo output::json("json", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "jsonp")
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
	elseif($g_cgival['t'] == "perl")
	{
		$string = php_to_perl::php_array_to_perl_hash($g_show);
		$string = mb_convert_encoding($string, "GBK", "UTF-8");
		echo $string;
	}
	elseif($g_cgival['t'] == "iframe")
	{
		header("location:{$g_cgival['link']}");
	}
	else
	{
		echo output::json("jsonp", $g_show, $g_cgival['callback']);
	}
}